import 'package:get/get.dart';
import 'package:ride_rex/net/common_net.dart';
import 'package:ride_rex/net/common_paging.dart';
import 'package:ride_rex/person_detail/person_detail_logic.dart';
import 'package:ylm_ui_kit/ylm_ui_kit.dart';

import '../../../net/common_global_url.dart';

class ApiMineFollowList extends CommonApi {
  ApiMineFollowList.list() : super(CommonGlobalUrl.getFriendsList);
}

class MineFollowListLogic extends GetxController with PullToRefresh<MineFollowListBean>, OnceClick {
  List<MineFollowListBean> dataList = [];

  @override
  void onReady() {
    super.onReady();
    firstDataAnim();
  }

  @override
  String method() => 'POST';

  @override
  MineFollowListBean decodeJson(json) => MineFollowListBean.fromJson(json);

  @override
  Future<CommonApi> getApi() async => ApiMineFollowList.list();

  @override
  void loadResult(bool isFirstPage, List<MineFollowListBean> temp, originalJson) {
    if (isFirstPage) {
      dataList.clear();
    }
    dataList.addAll(temp);
    update();
  }

  Future unfollow(String userId) async {
    if (userId.empty) return;
    startOnce(() async {
      CommonLogic.unfollow(userId);
      dataList.removeWhere((element) => element.userId == userId);
      update();
      Loading.toast('unFollow success', closeLoading: true);
    });
  }
}

class MineFollowListBean {
  String? userId;
  int? userType;
  String? nickname;
  String? avatar;
  String? avatarUrl;
  int? gender;
  String? birthday;
  int? age;
  String? language;
  String? country;
  String? about;
  bool? isSpecialFollow;
  String? onlineStatus;
  int? level;
  bool? isVip;

  MineFollowListBean(
      {this.userId,
      this.userType,
      this.nickname,
      this.avatar,
      this.avatarUrl,
      this.gender,
      this.birthday,
      this.age,
      this.language,
      this.country,
      this.about,
      this.isSpecialFollow,
      this.onlineStatus,
      this.level,
      this.isVip});

  MineFollowListBean.fromJson(Map<String, dynamic> json) {
    if (json["userId"] is String) {
      userId = json["userId"];
    }
    if (json["userType"] is num) {
      userType = (json["userType"] as num).toInt();
    }
    if (json["nickname"] is String) {
      nickname = json["nickname"];
    }
    if (json["avatar"] is String) {
      avatar = json["avatar"];
    }
    if (json["avatarUrl"] is String) {
      avatarUrl = json["avatarUrl"];
    }
    if (json["gender"] is num) {
      gender = (json["gender"] as num).toInt();
    }
    if (json["birthday"] is String) {
      birthday = json["birthday"];
    }
    if (json["age"] is num) {
      age = (json["age"] as num).toInt();
    }
    if (json["language"] is String) {
      language = json["language"];
    }
    if (json["country"] is String) {
      country = json["country"];
    }
    if (json["about"] is String) {
      about = json["about"];
    }
    if (json["isSpecialFollow"] is bool) {
      isSpecialFollow = json["isSpecialFollow"];
    }
    if (json["onlineStatus"] is String) {
      onlineStatus = json["onlineStatus"];
    }
    if (json["level"] is num) {
      level = (json["level"] as num).toInt();
    }
    if (json["isVip"] is bool) {
      isVip = json["isVip"];
    }
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> _data = <String, dynamic>{};
    _data["userId"] = userId;
    _data["userType"] = userType;
    _data["nickname"] = nickname;
    _data["avatar"] = avatar;
    _data["avatarUrl"] = avatarUrl;
    _data["gender"] = gender;
    _data["birthday"] = birthday;
    _data["age"] = age;
    _data["language"] = language;
    _data["country"] = country;
    _data["about"] = about;
    _data["isSpecialFollow"] = isSpecialFollow;
    _data["onlineStatus"] = onlineStatus;
    _data["level"] = level;
    _data["isVip"] = isVip;
    return _data;
  }

  MineFollowListBean copyWith({
    String? userId,
    int? userType,
    String? nickname,
    String? avatar,
    String? avatarUrl,
    int? gender,
    String? birthday,
    int? age,
    String? language,
    String? country,
    String? about,
    bool? isSpecialFollow,
    String? onlineStatus,
    int? level,
    bool? isVip,
  }) =>
      MineFollowListBean(
        userId: userId ?? this.userId,
        userType: userType ?? this.userType,
        nickname: nickname ?? this.nickname,
        avatar: avatar ?? this.avatar,
        avatarUrl: avatarUrl ?? this.avatarUrl,
        gender: gender ?? this.gender,
        birthday: birthday ?? this.birthday,
        age: age ?? this.age,
        language: language ?? this.language,
        country: country ?? this.country,
        about: about ?? this.about,
        isSpecialFollow: isSpecialFollow ?? this.isSpecialFollow,
        onlineStatus: onlineStatus ?? this.onlineStatus,
        level: level ?? this.level,
        isVip: isVip ?? this.isVip,
      );
}
